package lu.med.praxiswahl.wahlumed.client.kalender;

import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.table.AbstractTableModel;

import lu.med.praxiswahl.wahlumed.client.utilities.ContextFactoryRemote;
import lu.med.praxiswahl.wahlumed.client.utilities.ContextFactoryRemoteInt;
import lu.med.praxiswahl.wahlumed.client.utilities.UniCode;
import lu.med.praxiswahl.wahlumed.model.beans.CalendarTBL;
import lu.med.praxiswahl.wahlumed.model.beans.CustomerTBL;
import lu.med.praxiswahl.wahlumed.service.beans.CalendarService;
import lu.med.praxiswahl.wahlumed.service.beans.CustomerService;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.CalendarServiceInt;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.CustomerServiceInt;

@SuppressWarnings("serial")
public class AlleTermineTableModel extends AbstractTableModel
{
    private ArrayList<KalenderTableDataRow> data = new ArrayList<KalenderTableDataRow>();

    public AlleTermineTableModel(String matricule, String von, String bis)
    {
	// Fragt alle Termine bei der Datenbank ab und fügt sie zum Modell
	// hinzu
	data.clear();

	ContextFactoryRemoteInt remote_context = new ContextFactoryRemote();
	CalendarServiceInt cal = remote_context
		.getRemoteContextCalendarService();

	CalendarTBL[] tbl = cal.getAllAppointmentOf_Customer(matricule, von,
		bis);

	int calObjLength = tbl.length;

	if (calObjLength == 0)
	{
	    this.fireTableDataChanged();
	    return;
	}

	for (int i = 0; i < calObjLength; i++)
	{
	    cal.getA_CalendarInstance(tbl[i]);

	    KalenderTableDataRow curData = new KalenderTableDataRow();
	    curData.setAnkunft(cal.getAnkunft());
	    curData.setHcash("");
	    curData.setHerstellt("");
	    curData.setHkarte("");

	    CustomerServiceInt cust = remote_context
		    .getRemoteContextCustomerService();
	    cust = new CustomerService();
	    cust.getCustomer(cal.getPatientin().getMatrikel_nummer());
	    curData.setMatricule(cal.getPatientin().getMatrikel_nummer());
	    curData.setName(cust.getNachname(matricule)[0] + " "
		    + cust.getNachname(matricule)[1] + " "
		    + cust.getNachname(matricule)[2]);
	    curData.setRvAenderung(cal.getRV_Aenderung());
	    curData.setSortie(cal.getSortie());
	    curData.setTelefon(cust.getPrivatTel(matricule));
	    String dStr = "";
	    int m, y, d;
	    m = cal.getDatum().get(Calendar.MONTH) + 1;
	    y = cal.getDatum().get(Calendar.YEAR);
	    d = cal.getDatum().get(Calendar.DAY_OF_MONTH);
	    String mm, dd;
	    if (m < 10)
		mm = "0" + m;
	    else
		mm = "" + m;
	    if (d < 10)
		dd = "0" + d;
	    else
		dd = "" + d;
	    dStr = dd + "." + mm + "." + y;
	    curData.setUhrzeit(dStr + "; " + cal.getTerminUrhZeit());
	    curData.setUsBeginn(cal.getUS_Beginn());
	    curData.setUsTyp(cal.getUS_Typ());
	    curData.setWartezeit(cal.getWarteZeit());
	    curData.setNewPatient(cal.getNewCustomer_GreenColor());

	    data.add(curData);
	}

	this.fireTableDataChanged();
    }
    
    public AlleTermineTableModel(String matricule)
    {
	// Fragt alle Termine bei der Datenbank ab und fügt sie zum Modell
	// hinzu
	data.clear();
	
	ContextFactoryRemoteInt remote_context = new ContextFactoryRemote();
	CalendarServiceInt cal = remote_context
		.getRemoteContextCalendarService();

	CalendarTBL[] tbl = cal.getAllAppointmentOf_Customer(matricule);

	int calObjLength = tbl.length;

	if (calObjLength == 0)
	{
	    this.fireTableDataChanged();
	    return;
	}

	for (int i = 0; i < calObjLength; i++)
	{
	    /*
	    cal.getA_CalendarInstance(tbl[i]);

	    KalenderTableDataRow curData = new KalenderTableDataRow();
	    curData.setAnkunft(cal.getAnkunft());
	    curData.setHcash("");
	    curData.setHerstellt("");
	    curData.setHkarte("");

	    CustomerServiceInt cust = remote_context
		    .getRemoteContextCustomerService();
	    cust = new CustomerService();
	    cust.getCustomer(cal.getPatientin().getMatrikel_nummer());
	    curData.setMatricule(cal.getPatientin().getMatrikel_nummer());
	    curData.setName(cust.getNachname(matricule)[0] + " "
		    + cust.getNachname(matricule)[1] + " "
		    + cust.getNachname(matricule)[2]);
	    curData.setRvAenderung(cal.getRV_Aenderung());
	    curData.setSortie(cal.getSortie());
	    curData.setTelefon(cust.getPrivatTel(matricule));
	    String dStr = "";
	    int m, y, d;
	    m = cal.getDatum().get(Calendar.MONTH) + 1;
	    y = cal.getDatum().get(Calendar.YEAR);
	    d = cal.getDatum().get(Calendar.DAY_OF_MONTH);
	    String mm, dd;
	    if (m < 10)
		mm = "0" + m;
	    else
		mm = "" + m;
	    if (d < 10)
		dd = "0" + d;
	    else
		dd = "" + d;
	    dStr = dd + "." + mm + "." + y;
	    curData.setUhrzeit(dStr + "; " + cal.getTerminUrhZeit());
	    curData.setUsBeginn(cal.getUS_Beginn());
	    curData.setUsTyp(cal.getUS_Typ());
	    curData.setWartezeit(cal.getWarteZeit());
	    curData.setNewPatient(cal.getNewCustomer_GreenColor());
	    */

	    KalenderTableDataRow curData = new KalenderTableDataRow();
	    curData.setAnkunft(tbl[i].getAnkunft_zeit());
	    curData.setHcash(tbl[i].getHCash());
	    curData.setHerstellt(tbl[i].getHErstellt());
	    curData.setHkarte(tbl[i].getHKarte());

	    CustomerTBL cust = tbl[i].getCustomer();
	    
	    curData.setMatricule(cust.getMatrikel_nummer());
	    curData.setName(cust.getNachname_1()+ " "
		    + cust.getNachname_2()+ " "
		    + cust.getNachname_3());
	    
	    curData.setRvAenderung(tbl[i].getRv_aenderung());
	    curData.setSortie(tbl[i].getSortie());
	    curData.setTelefon(cust.getTelefon());
	    String dStr = "";
	    int m, y, d;
	    m = tbl[i].getDatum().get(Calendar.MONTH) + 1;
	    y = tbl[i].getDatum().get(Calendar.YEAR);
	    d = tbl[i].getDatum().get(Calendar.DAY_OF_MONTH);
	    String mm, dd;
	    if (m < 10)
		mm = "0" + m;
	    else
		mm = "" + m;
	    if (d < 10)
		dd = "0" + d;
	    else
		dd = "" + d;
	    dStr = dd + "." + mm + "." + y;
	    curData.setUhrzeit(dStr + "; " + tbl[i].getRv_uhrzeit());
	    curData.setUsBeginn(tbl[i].getUntersuchungsbeginn());
	    curData.setUsTyp(tbl[i].getUntersuchungstyp());
	    curData.setWartezeit(tbl[i].getWartezeit());
	    curData.setNewPatient(tbl[i].getNewCustomer_GreenColor());


	    data.add(curData);
	}

	this.fireTableDataChanged();
    }

    public int getColumnCount()
    {
	return 12;
    }

    @Override
    public String getColumnName(int columnIndex)
    {
	switch (columnIndex)
	{
	case 0:
	    return "RV";
	case 1:
	    return "Name";
	case 2:
	    return "Telefon";
	case 3:
	    return "US-Typ";
	case 4:
	    return "RV-" + UniCode.getAE() + "nderung";
	case 5:
	    return "Ankunft";
	case 6:
	    return "US-Beginn";
	case 7:
	    return "Wartezeit";
	case 8:
	    return "Sortir";
	case 9:
	    return "H-Cash";
	case 10:
	    return "H-Karte";
	case 11:
	    return "H-Erstellt";
	}
	return "";
    }

    public int getRowCount()
    {
	return data.size();
    }

    public Object getValueAt(int row, int column)
    {
	switch (column)
	{
	case 0:
	    return data.get(row).getUhrzeit();
	case 1:
	    return data.get(row).getName();
	case 2:
	    return data.get(row).getTelefon();
	case 3:
	    return data.get(row).getUsTyp();
	case 4:
	    return data.get(row).getRvAenderung();
	case 5:
	    return data.get(row).getAnkunft();
	case 6:
	    return data.get(row).getUsBeginn();
	case 7:
	    return data.get(row).getWartezeit();
	case 8:
	    return data.get(row).getSortie();
	case 9:
	    return data.get(row).getHcash();
	case 10:
	    return data.get(row).getHkarte();
	case 11:
	    return data.get(row).getHerstellt();
	}
	return null;
    }
}
